package com.mlethe.library.recyclerview.viewholder;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

/**
 * 方便 DataBinding 的使用
 *
 * @author Mlethe
 * @date 2020/9/15
 */
public class BindingViewHolder extends ViewHolder {

    private ViewDataBinding binding;

    public BindingViewHolder(@NonNull View itemView) {
        super(itemView);
        binding = DataBindingUtil.bind(itemView);
    }

    /**
     * 绑定数据
     *
     * @param variableId
     * @param object
     * @return
     */
    public BindingViewHolder setVariable(int variableId, Object object) {
        binding.setVariable(variableId, object);
        return this;
    }

    /**
     * 此方法必须执行在UI线程，当绑定的数据修改时更新视图
     *
     * @return
     */
    public BindingViewHolder executePendingBindings() {
        binding.executePendingBindings();
        return this;
    }
}
